//
//  shopBrandView.m
//  ILiangCang
//
//  Created by ZX on 16-4-16.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "shopBrandView.h"
#import "brandCell.h"
#import "dataModel.h"
#import "UIImageView+WebCache.h"
#import "httpEngine.h"
#import "MJRefresh.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@implementation shopBrandView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
        [self addTableView];
        _currentPage = 0;
        
        
    }

    return self;
}

-(void)addTableView
{
    
    tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-136)];
    tabView.delegate =self;
    tabView.dataSource = self;
    [self addSubview:tabView];
    [tabView registerClass:[brandCell class] forCellReuseIdentifier:@"brandCell"];
    tabView.rowHeight = 60;


    [tabView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    [tabView addFooterWithTarget:self action:@selector(footerRefreshing)];
    [httpEngine requestToGetShopBrandWith:0 Completion:^(NSMutableArray *brandArray, NSError *error) {
        _dataArray = brandArray;
        [tabView reloadData];

    }];
    
    
}

#pragma mark----上下拉刷新表的方法
-(void)headerRefreshing
{
    _currentPage = 0;
    [httpEngine requestToGetShopBrandWith:self.currentPage Completion:^(NSMutableArray *brandArray, NSError *error) {
        _dataArray = brandArray;
        [tabView reloadData];
        
    }];
    [tabView headerEndRefreshing];
}

-(void)footerRefreshing
{
    self.currentPage = _currentPage+10;
    [httpEngine requestToGetShopBrandWith:self.currentPage Completion:^(NSMutableArray *brandArray, NSError *error) {
        
        if (error)
        {
            return ;
        }
        if (brandArray.count>0)
        {
            for (int i=0; i<brandArray.count; i++)
            {
                [_dataArray addObject:brandArray[i]];
                [tabView reloadData];
            }

        }else
        {
            NSLog(@"没了");
        }
        
    }];
    
    [tabView footerEndRefreshing];
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * indentifer = [NSString stringWithFormat:@"brandCell"];
    brandCell * cell = [tableView dequeueReusableCellWithIdentifier:indentifer forIndexPath:indexPath];
    dataModel * data = _dataArray[indexPath.row];
    if (!cell)
    {
        cell = [[brandCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"brandCell"];
        
    }
    [cell.image sd_setImageWithURL:data.headerURL];
    cell.title.text = data.name;
    
    return cell;
    
    
}


@end
